package com.android.kotlincoroutinemvvmdemo
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.kotlincoroutinemvvmdemo.adapter.DayNightModeAdapter
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityDayNightModeBinding
import com.android.kotlincoroutinemvvmdemo.itemdecoration.StackItemDecoration
import com.android.kotlincoroutinemvvmdemo.viewmodel.OneFiViewModel
import kotlinx.coroutines.launch

class DayNightModeActivity : BaseActivity<ActivityDayNightModeBinding>() {

    private lateinit var mAdapter: DayNightModeAdapter

    private lateinit var mViewModel: OneFiViewModel

    private lateinit var mDecoration: StackItemDecoration

    override fun getLayoutId() = R.layout.activity_day_night_mode

    override fun init(savedInstanceState: Bundle?) {

        mViewModel = ViewModelProvider(this)[OneFiViewModel::class.java]

        mDecoration = StackItemDecoration(this@DayNightModeActivity)

        mAdapter = DayNightModeAdapter(this@DayNightModeActivity)
        mBinding.rvDayNightMode.apply {
            layoutManager = LinearLayoutManager(this@DayNightModeActivity)
            adapter = mAdapter
            addItemDecoration(mDecoration)
        }

        mViewModel.getOneFiDataSet()

        lifecycleScope.launch {
            mViewModel.mOneFiFlow.collect {
                mAdapter.setDataSet(it)
            }
        }
    }

    override fun adapterUIModeNightForActivity(uiModeNight: Boolean) {
        super.adapterUIModeNightForActivity(uiModeNight)

        if (uiModeNight) {
            mBinding.dayNightModeRoot.setBackgroundColor(resources.getColor(R.color.black))
        } else {
            mBinding.dayNightModeRoot.setBackgroundColor(resources.getColor(R.color.white))
        }

        //只添加了一个ItemDecoration,获取更新下画笔颜色就ok了.实际上可以添加多个ItemDecoration.
        val itemDecoration = mBinding.rvDayNightMode.getItemDecorationAt(0) as StackItemDecoration
        itemDecoration.updatePaintColor(uiModeNight)
        Log.d(TAG, "---count000---:${mBinding.rvDayNightMode.itemDecorationCount}")

        mAdapter.adapterUIModeNightForRecyclerView(uiModeNight)

        adapterStatusBar(R.color.purple_500)
    }

    companion object {
        const val TAG = "DayNightModeActivity"
    }
}